<template>
  <div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
    <el-table
        :data="shopList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
    >
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column align="center" label="店铺名称" width="95">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="店铺介绍">
        <template slot-scope="scope">
          {{ scope.row.introdce }}
        </template>
      </el-table-column>
      <el-table-column label="店铺类别" width="110" align="">
        <template slot-scope="scope">
          <span>{{ scope.row.category }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺地点" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.place }}
        </template>
      </el-table-column>
      <el-table-column label="店铺状况" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status"
                     :active-value="1"
                     :inactive-value="0"
                     @change=switchStatusMe($event,scope.row.id,scope.row.status)>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="店铺商标" width="95">
        <template slot-scope="scope">
          <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                    :src="getImage(scope.row.icon)"
                    :preview-src-list="[ `http://localhost:9090/hbgc/download?name=${scope.row.icon}` ]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.person }}
        </template>
      </el-table-column>
      <el-table-column label="销量" width="110" align="center">
        1
      </el-table-column>
      <el-table-column label="联系电话" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" append-to-body>
      <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="80px">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入店铺名称"/>
        </el-form-item>


        <el-form-item label="店铺类别" prop="category">
          <el-select v-model="form.category" placeholder="请输入店铺类别">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="店铺地点" prop="place">
          <el-select v-model="form.place" placeholder="请输入店铺地点">
            <el-option
                v-for="item in options3"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="联系人" prop="person">
          <el-input v-model="form.person" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="店铺介绍" prop="introdce">
          <el-input v-model="form.introdce" placeholder="请输入店铺介绍" type="textarea"/>
        </el-form-item>
        <el-form ref="form" :model="form" v-model="form.icon" :rules="rules" label-width="80px">
          <el-form-item label="图片">

            <el-upload class="upload-demo"
                       ref="upload"
                       action="http://localhost:9090/hbgc/upload"
                       :before-remove="beforeRemove"
                       name="file"
                       :limit="1"
                       :on-exceed="handleExceed"
                       :on-success="handleAvatarSuccess"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传一张jpg/png文件</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "MyShop",
  data(){
    return{
      options: [{
        value: '米饭炒菜',
        label: '米饭炒菜'
      }, {
        value: '拉面拌面',
        label: '拉面拌面'
      }, {
        value: '炒饭炒饼',
        label: '炒饭炒饼'
      }, {
        value: '米线米粉',
        label: '米线米粉'
      }, {
        value: '汉堡炸鸡',
        label: '汉堡炸鸡'
      }, {
        value: '烤肉炸串',
        label: '烤肉炸串'
      }, {
        value: '奶茶饮料',
        label: '奶茶饮料'
      }, {
        value: '其他类型',
        label: '其他类型'
      }],
      options3: [{
        value: '新食堂',
        label: '新食堂'
      }, {
        value: '旧食堂',
        label: '旧食堂'
      }],
      shopList: [],
      form: {},
      rules: {
        phone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur'},
          {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: '请输入正确的手机号码'
          }
        ],
      },
      shopId:0,
      title: '',
      dialogFormVisible: false,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.selectById()
  },
  methods:{
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.username)).data
    },
    handleAdd(){
      if (this.shopList.length<=0){
        this.title = "添加"
        this.dialogFormVisible = true
        this.$refs.upload.clearFiles()
      }else {
        this.$message.info("已有店铺不能添加")
      }
    },
    switchStatusMe($event,id,status){
      console.log(status)
      this.request.put("/updateStatus/"+id+'/'+status).then(res=>{
        console.log(res)
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    submitForm() {
      if (this.form.id != undefined) {
        this.request.put("/updateShop", this.form).then(res => {
          if (res) {
            this.$message.success("修改成功")
            this.dialogFormVisible = false
            this.selectListPage()
          } else {
            this.$message.error("修改失败")
          }
        })}else {
        const id=  JSON.parse(localStorage.getItem("user")).id;
        this.request.post("/MyShopAdd/"+id,this.form).then(res=>{
          if (res) {
            this.$message.success("修改成功")
            this.dialogFormVisible = false
            this.getUser().then(res => {
              res.avatarUrl = this.form.avatarUrl
              res.token = JSON.parse(localStorage.getItem("user")).token
              localStorage.setItem("user", JSON.stringify(res))
            })
            this.$nextTick(()=>{
              location.reload();
            });
          } else {
            this.$message.error("修改失败")
          }


        })
      }

    },
    handleAvatarSuccess(res, file) {
      this.form.icon = res.data;
    },
    beforeRemove(file) {

      return this.$confirm(`确定移除 ${file.name}？`).then(() => {
        this.request.delete("/deleteImg/" + this.form.icon)
      })
    },
    handleExceed() {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    selectById(){
   this.shopId= JSON.parse(localStorage.getItem("user")).shopId
      this.request.get("/mySelectShopId/"+this.shopId).then(res=>{
        this.shopList=res.data
      })
    },
    handleEdit(row) {
      this.title = "修改"
      this.form = row
      this.dialogFormVisible = true
      this.$refs.upload.clearFiles()
    },
    getImage(image) {
      return `http://localhost:9090/hbgc/download?name=${image}`
    },
  }
}
</script>

<style scoped>

</style>